<template>
  <div class="add-form">
    <el-page-header @back="$router.back()" :content="`${form.id ? '编辑':'添加'}`">
    </el-page-header>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="margin-top: 20px;">
      <el-form-item label="用户角色">
        <RoleSelect v-model="form.roles"></RoleSelect>
      </el-form-item>

      <el-form-item label="登录账号" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="登录密码">
        <el-input v-model="form.password"  show-password></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.realname"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>

      <el-form-item label="头像">
        <FileUpload v-model="form.avatar"></FileUpload>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio v-model="form.status" label="1">禁用</el-radio>
        <el-radio v-model="form.status" label="0">正常</el-radio>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
  import RoleSelect from './components/RoleSelect.vue'
  import FileUpload from '../common/components/FileUpload.vue'
  import {ajaxPost} from '@/api/ajax'
  export default {
    components:{
      RoleSelect,
      FileUpload
    },
    data() {
      return {
        form: {
          id: '',
          roles:'',
          username: '',
          realname: '',
          phone: '',
          password: '',
          avatar: '',
          status: '0',
        },
        rules: {
          username: [{
            required: true,
            message: '请输入登录账号',
            trigger: 'blur'
          }, ],
          password: [{
            required: true,
            message: '请输入登录密码',
            trigger: 'blur'
          }, ],
        },
        row: [],
      }
    },
    created() {
      if (this.$route.params.id) {
        this.form = this.$route.params;
      }
    },
    methods: {
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let data = { ...this.form
            };
            if (data.id) {
              ajaxPost('admin/update', data).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '编辑成功',
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.$router.back();
                    },
                  })
                }
              })
            } else {
              ajaxPost('admin/create', data).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '添加成功',
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.$router.back();
                    },
                  })
                }
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style lang="scss">
  .add-form {
    padding: 20px;
    box-sizing: border-box;
    /* input {
      width: 500px;
    } */
  }
</style>
